package edu.centenary.centenaryController;

public class TreeNode{
	//datamembers
	private BodyPart bp;
	private TreeNode t1;
	private TreeNode t2;
	
	//constructor
	public TreeNode(BodyPart bp){
		this.bp = bp;
	}
	
	public TreeNode(TreeNode t1, TreeNode t2){
		this.t1 = t1;
		this.t2 = t2;
	}
	//methods
	public String toString(){
		return this.printTree("");
	}
	
	public TreeNode left(){
		return t1;
	}
	
	public TreeNode right(){
		return t2;
	}
	
	public String printTree(String prefix){
		String s = "";
		if(t1 != null){
			s+= t1.printTree(prefix+"   ");
		}
		if(bp != null){
			s+= prefix+bp.toString()+"\n";
		}
		else{
			s+= prefix+"+\n";
		}
		if(t2 != null){
			s+= t2.printTree(prefix+"   ");
		}
		return s;
	}
	
	public int numChests(){
		if(t1 == null && t2 == null){
			if(this.bp.getBodyPart().equals("Chest")){
				return 1;
			}else{
				return 0;
			}
		} else{
			int c = 0;
			if(t1 != null){
				c+= t1.numChests();
			}
			if(t2 != null){
				c+= t2.numChests();
			}
			return c;
		}
	}
	
	public double distance(TreeNode t3){
		if(this.bp != null && t3.bp != null){
			return this.bp.distance(t3.bp);
		}
		else if(this.bp == null && t3.bp != null){
			double ab = t1.distance(t2);
			double ac = t1.distance(t3);
			double bc = t2.distance(t3);
			return (ac + bc - ab )/ 2;
		}
		else if(t3.bp == null && this.bp != null){
			double ab = t3.t1.distance(t3.t2);
			double ac = t3.t1.distance(this);
			double bc = t3.t2.distance(this);
			return (ac + bc - ab )/ 2;
		}
		else{
			double ac = t1.distance(t3.t1);
			double bc = t2.distance(t3.t1);
			double ab = t1.distance(t2);
			double ad = t1.distance(t3.t2);
			double bd = t2.distance(t3.t2);
			double cd = t3.t1.distance(t3.t2);
			double first = ((ac + bc - ab) /2);
			double second = ((ad + bd - ab) /2);
			return (first + second - cd)/2;
		}
	}
	

}